/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../../../../engine/control/XButton");
const { X2DBeta } = require("../../../../engine/graphics/X2DInterface");
const { Scr } = require("../../../../engine/XDefine");
const { XTools } = require("../../../../engine/XTools");
const { ProjectConfig } = require("../../ProjectConfig");

class RosBase {
    constructor(name, type) {
        this.name = name;
        this.type = type;
        this.x = 0;
        this.y = 0;
        this.w = Math.max(X2DBeta.getTextWidth(name, 12) + 8, 200);
        this.h = 32;
        this.draging = {
            is: false,
            x: 0,
            y: 0,
        };
        this.moved = false;
        this.hide = false;
        this.renderFunc = "";
        this.publishFunc = "";
        let attr = ProjectConfig.getInstance().getRuntimeAttr(this.type, this.name);
        if (attr) {
            this.hide = attr.hide;
            this.renderFunc = attr.code;
            this.publishFunc = attr.code2;
            this.x = attr.x;
            this.y = attr.y;
            this.moved = true;
        }

        this.lastClickTime = 0;

        this.btns = [];
        this.btn_hide = new XButton({
            name: "隐藏",
            w: 60,
            h: 14,
            onClicked: () => {
                this.hide = !this.hide;
                ProjectConfig.getInstance().setRuntimeAttr(this);
            },
            onDraw: (offx, offy) => {
                if (this.btn_hide.isDown()) {
                    X2DBeta.fillRect(this.btn_hide.leftX() + offx, this.btn_hide.topY() + offy, this.btn_hide.width(), this.btn_hide.height(), 0xff808080, 1);
                }
                else if (this.btn_hide.isOn()) {
                    X2DBeta.fillRect(this.btn_hide.leftX() + offx, this.btn_hide.topY() + offy, this.btn_hide.width(), this.btn_hide.height(), 0xff404040, 1);
                }
                X2DBeta.drawRect(this.btn_hide.leftX() + offx, this.btn_hide.topY() + offy, this.btn_hide.width(), this.btn_hide.height(), 0xff00ff00, 1);
                X2DBeta.drawText(this.hide ? "取消隐藏" : "隐藏", 12, this.btn_hide.centerX() + offx, this.btn_hide.centerY() + offy, 1, 1, 0, -2, -2, 0xff00ff00);
            }
        })
        this.asyncData = {};
    }
    renderButton(btn, offx, offy) {
        let rect = btn.rect();
        rect.x += offx;
        rect.y += offy;
        if (btn.isDown()) {
            X2DBeta.fillRect(rect.x, rect.y, rect.w, rect.h, 0xff808080, 1);
        }
        else if (btn.isOn()) {
            X2DBeta.fillRect(rect.x, rect.y, rect.w, rect.h, 0xff404040, 1);
        }
        X2DBeta.drawRect(rect.x, rect.y, rect.w, rect.h, 0xff00ff00, 1);
        X2DBeta.drawText(btn.name_, 12, rect.x + rect.w / 2, rect.y + rect.h / 2, 1, 1, 0, -2, -2, 0xff00ff00);
    }
    move(x, y) {
        this.x = x;
        this.y = y;
        this.moved = true;
    }
    inX() {
        return this.x// + 4;
    }
    inY() {
        return this.y + 4;
    }
    outX() {
        return this.x + this.w// - 4;
    }
    outY() {
        return this.y + this.h - 4;
    }
    outX2() {
        return this.x + this.w// - 4;
    }
    outY2() {
        return this.y + 4;
    }
    centerX() {
        return this.x + this.w / 2;
    }
    centerY() {
        return this.y + this.h / 2;
    }
    onTouch(msg, x, y, locked = false, hideOn = true) {
        if (hideOn && this.hide) {
            return false;
        }
        if (this.draging.is) {
            if (msg == 2) {
                let dx = x - this.draging.x;
                let dy = y - this.draging.y;
                this.x += dx;
                this.y += dy;
                if (XTools.KEY_CTRL) {//同时移动
                    const { RuntimeManager } = require("../RuntimeManager");
                    RuntimeManager.getInstance().moveTegather(this, dx, dy, false);
                }
                this.draging.x = x;
                this.draging.y = y;
                return true;
            } else if (msg == 3) {
                ProjectConfig.getInstance().setRuntimeAttr(this);
                if (XTools.KEY_CTRL) {//同时移动
                    const { RuntimeManager } = require("../RuntimeManager");
                    RuntimeManager.getInstance().moveTegather(this, 0, 0, true);
                }
            }
            this.draging.is = false;
            return true;
        }
        if (locked) {
            for (let btn of this.btns) {
                if (btn.onTouch(msg, x, y)) {
                    return true;
                }
            }
        }
        if (msg == 1) {
            if (XTools.InRect(x, y, this.x, this.y, this.w, this.h)) {
                if (XTools.TIME_MS - this.lastClickTime < 300) {
                    this.onDoubleClicked(x, y);
                    this.lastClickTime = 0;
                } else {
                    this.lastClickTime = XTools.TIME_MS;
                    this.draging.is = true;
                    this.draging.x = x;
                    this.draging.y = y;
                }
                return true;
            }
        }
        return false;
    }
    onDoubleClicked(x, y) {

    }
    onKey(type, k) {
        if (k == 'h' || k == 'H') {
            this.hide = !this.hide;
            ProjectConfig.getInstance().setRuntimeAttr(this);
            return true;
        }
    }
}

module.exports = { RosBase };